// pages/newlyweds/address/addAddress/addAddress.js
const app = getApp();
import { add, edit } from '../../../api/address'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    aid: {
      type: [Object, String, Number]
    },
    info: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    region: ['江苏省', '南京市', '玄武区'],
    location: '江苏省,南京市,玄武区',
    name: '',
    phone: '',
    detail: '',
  },
  lifetimes: {
    attached(){ 
      const {aid, info} = this.data
      console.log(aid, info)
      if( aid){
        let region = [info.post_province, info.post_city, info.post_district]
        this.setData({
          region,
          location: region.join(','),
          name: info.post_receiver,
          phone: info.post_phone,
          detail: info.post_address,
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    changeItem: function (e) {
      const inputValueName = e.currentTarget.dataset.prop

      this.setData({
        [inputValueName]: e.detail.value
      })
      // this.tryEditFeedBackInfos()
  
    },    
    bindRegionChange: function (e) {
      console.log('picker发送选择改变，携带值为', e)
      this.setData({
        region: e.detail.value,
        location: e.detail.value.join(',')
      })
    },
    save() {
      const { name ,phone, detail,location, aid,region } =  this.data
      console.log(this.data)
      if(!name){
        wx.showToast({
          title: '请填写收获姓名',
          icon: 'none'
        })
        return 
      }
      if(!phone){
        wx.showToast({
          title: '请填写电话',
          icon: 'none'
        })
        return 
      }
      if( phone.length!==11){
        wx.showToast({
          title: '请输入正确的手机号码',
          icon: 'none'
        }) 
        return;
      }
      if(!detail){
        wx.showToast({
          title: '请填写详细地址',
          icon: 'none'
        })
        return 
      }
      const [post_province,post_city,post_district ] = region;
      const params = {
        post_phone: String(phone),
        post_address : String(detail),
        location,
        post_receiver: String(name),
        post_province,
        post_city,
        post_district,
      }
      if(aid!==null&&aid){
        params.receiver_id = String(aid)
        edit(params).then( res => {
       
          console.log(res)
          wx.showToast({
            title: '编辑地址成功',
            icon: 'none'
          })
          // console.log('res.data.address_id',res.data.address_id)
          this.triggerEvent('list' );
        })
        .catch( err => {
          wx.showToast({
            title: '编辑地址失败',
            icon: 'none'
          })
        })
      }
      else {
        add(params).then( res => {
       
          console.log(res)
          wx.showToast({
            title: '新增地址成功',
            icon: 'none'
          })
          this.triggerEvent('list',  res.data.address_id);
        })
        .catch( err => {
          wx.showToast({
            title: '新增地址失败',
            icon: 'none'
          })
        })

      }

      
    },
    close() {
      this.triggerEvent('close');

    },
    getPhone(){
      wx.showToast({
        title: '获取手机号',
      })
    }
  }
})